import pygame
from pygame.locals import *
from pygame import *
from guiinput import guiinput
from classes import *

class lmenu(pygame.sprite.Sprite):
    def __init__(self, items, application):
        joyconf = config.config("joywm.xml")
        themefile = joyconf.conf.get('theme')
        themeconf = config.config(themefile)
        themename = themeconf.conf.get('name')
        self.spriteitems = []
        self.lmenulist = []
        max_width = 0
        max_height = 0
        self.items = items
        self.x = 0
        self.visible = False
        self.number = 0
        self.select = None
        # Render all items
        for x in range(0, len(items)):
            items[x] = " " + items[x] + " "
            self.spriteitems.append(loadlabel(items[x], application.fontsize, application.fontcolor, application.font, application.fontbold, application.fontitalic))
            if self.spriteitems[x].rect.width > max_width:
                max_width = self.spriteitems[x].rect.width
            max_height += application.cubey
            if self.spriteitems[x].rect.width > max_width:
                max_width = self.spriteitems[x].rect.width
        # Load background
        background = loadback(application.themepath + 'widgets/lmenu_background.png', max_width, max_height, application)
        background.rect.x = 0
        self.by = background.rect.y
        # Load bar
        self.bar = loadback(application.themepath + 'widgets/lmenu_bar.png', max_width, application.cubey, application)
        self.bar.rect.x = background.rect.x
        self.bar.rect.y = self.by
        # Rearrange all menuitems
        s = background.rect.y
        for x in range(0, len(items)):
            self.spriteitems[x].rect.y = s
            s += application.cubey
        # Add all to self.lmenulist in correct order
        self.lmenulist.append(background)
        self.lmenulist.append(self.bar)
        self.lmenulist.append(self.spriteitems)
        # Render the frame
        # Lines
        # Line up
        lu = loadimage(application.themepath + 'widgets/frame_upper.png', max_width, application.cubex / 2)
        lu.rect.x = 0
        lu.rect.y = background.rect.y - application.cubex / 2
        # Line right
        lr = loadimage(application.themepath + 'widgets/frame_right.png', application.cubex / 2, max_height)
        lr.rect.x = max_width
        lr.rect.y = background.rect.y
        # Line bottom
        lb = loadimage(application.themepath + 'widgets/frame_bottom.png', max_width, application.cubex / 2)
        lb.rect.x = 0
        lb.rect.y = background.rect.y + background.rect.height
        # Corners
        # Corner upper right
        ur = loadimage(application.themepath + 'widgets/frame_upperright.png', application.cubex / 2, application.cubex / 2)
        ur.rect.x = background.rect.width
        ur.rect.y = background.rect.y - application.cubex / 2
        # Corner bottom right
        br = loadimage(application.themepath + 'widgets/frame_bottomright.png', application.cubex / 2, application.cubex / 2)
        br.rect.x = background.rect.width
        br.rect.y = background.rect.y + background.rect.height
        # Upper line
        self.lmenulist.append(lu)
        # Right line
        self.lmenulist.append(lr)
        # Bottom line
        self.lmenulist.append(lb)
        # Corner upper right
        self.lmenulist.append(ur)
        # Corner bottom right
        self.lmenulist.append(br)
        # Render all items in messagelist
        self.lmenurender = pygame.sprite.OrderedUpdates(self.lmenulist)

    def show(self, application):
        self.visible = True
        self.lmenurender.draw(pygame.display.get_surface())
        pygame.display.flip()
        device = "any"
        key = guiinput()
        while 1:
            key.getkey()
            if key.keyname == "down":
                self.x = self.x + 1
                if self.x > len(self.items)-1:
                    self.x = len(self.items)-1
                self.bar.rect.y = self.spriteitems[self.x].rect.y
                self.lmenurender.draw(pygame.display.get_surface())
                pygame.display.flip()
            if key.keyname == "up":
                self.x = self.x - 1
                if self.x < 0:
                    self.x = 0
                self.bar.rect.y = self.spriteitems[self.x].rect.y
                self.lmenurender.draw(pygame.display.get_surface())
                pygame.display.flip()
            if key.keyname == "l":
                self.hide(application)
                self.select = None
                break
            if key.keyname == "a":
                self.number = self.x
                self.select = self.items[self.number][1:-1]
                self.hide(application)
                break
	    key.delete()

    def hide(self, application):
        self.visible = False
        application.spritegrouprender.draw(pygame.display.get_surface())
        pygame.display.flip()
